package per.sen.shopping.infrastructure.configuration;

import io.quarkus.reactive.datasource.ReactiveDataSource;
import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.mysqlclient.MySQLPool;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

/**
 * @Describe: 轮询选择数据源(读操作)
 * @Author: LYS
 * @Date: 2024/6/15 23:42
 */
@ApplicationScoped
public class DynamicReactiveSqlClientSelector implements ReactiveSqlClientSelector {
    private int counter = 0;
    @Inject
    @ReactiveDataSource("slave1")
    MySQLPool slave1Client;
    @Inject
    @ReactiveDataSource("slave2")
    MySQLPool slave2Client;
    @Override
    public Uni<MySQLPool> selectClient(boolean readOnly) {
        counter = (counter + 1) % 2;
        if(counter == 0){
            return Uni.createFrom().item(slave1Client);
        }else{
            return Uni.createFrom().item(slave2Client);
        }
    }
}
